<?php
/**
 * 地铁模型
 */

namespace app\api\model;


use app\common\model\BaseModel;
use PDOStatement;
use think\Collection;
use think\Db;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\exception\DbException;

class Metro extends BaseModel {

    /**
     * 地铁树
     * @param $params
     * @return array|PDOStatement|string|Collection
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     * @throws DbException
     */
    public static function getList($params) {
        //市下面的全部地铁
        $region_data = Db::name('region')
            ->where('city_code', $params['city_code'])
            ->where('rank', 2)
            ->find();
        $codes = $region_data['p_codes'] . $params['city_code'] . ',';
        $data = db('metro')
            ->where('city_codes', 'like', trim($codes) . '%')
            ->field('pid,name,lng,lat,id')
            ->order('metro_sort', 'asc')
            ->select();
        $data = self::listToTree($data);
        foreach ($data as $k => $v) {
            unset($data[$k]['id']);
            unset($data[$k]['pid']);
            unset($data[$k]['lng']);
            unset($data[$k]['lat']);
            if (!empty($data[$k]['children'])) {
                foreach ($data[$k]['children'] as $k1 => $v1) {
                    unset($data[$k]['children'][$k1]['id']);
                    unset($data[$k]['children'][$k1]['pid']);
                    $data[$k]['children'][$k1]['lng'] = !empty($data[$k]['children'][$k1]['lng']) ? (string)$data[$k]['children'][$k1]['lng'] : '';
                    $data[$k]['children'][$k1]['lat'] = !empty($data[$k]['children'][$k1]['lat']) ? (string)$data[$k]['children'][$k1]['lat'] : '';
                }
            }
        }
        return $data;
    }

    /**
     * 树
     * @param $list
     * @param string $pk
     * @param string $pid
     * @param string $child
     * @param int $root
     * @return array
     */
    public static function listToTree($list, $pk = 'id', $pid = 'pid', $child = 'children', $root = 0) {
        $tree = array();
        if (is_array($list)) {
            $refer = array();
            foreach ($list as $key => $data) {
                $refer[$data[$pk]] = &$list[$key];
            }
            foreach ($list as $key => $data) {
                // 判断是否存在parent
                $parentId = $data[$pid];
                if ($root == $parentId) {
                    $tree[$data[$pk]] = &$list[$key];
                } else {
                    if (isset($refer[$parentId])) {
                        $parent = &$refer[$parentId];
                        $parent[$child][$data[$pk]] = &$list[$key];
                        $parent[$child] = array_values($parent[$child]);
                    }
                }
            }
        }
        return array_values($tree);
    }

    /**
     * 城市是否有地铁
     * @param $params
     * @return int
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     * @throws DbException
     */
    public static function has_data($params) {
        $region_data = Db::name('region')
            ->where('city_code', $params['city_code'])
            ->where('rank', 2)
            ->find();
        $codes = $region_data['p_codes'] . $params['city_code'] . ',';
        $data = db('metro')
            ->where('city_codes', 'like', trim($codes) . '%')
            ->count();
        if ($data) {
            return 1;
        } else {
            return 2;
        }
    }

}
